﻿using Library.Core.Application.IInfrastructure;
using Library.Core.Infrastructure.Common;
using Library.Core.Infrastructure.EntityFrameworkDataAccess;
using Library.Core.Infrastructure.EntityFrameworkDataAcceSynthetic.Extension;
using Microsoft.EntityFrameworkCore;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using Synthesize.Infrastructure.PersistentObjects.Authority;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Synthesize.Infrastructure.Repository.Authority
{
    /// <summary>
    /// 部门基础类
    /// </summary>
    public class DepartmentRepository : RepositoryBase<DepartmentInfoEntity, DepartmentInfo>, IDepartmentRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currentUserInfo"></param>
        /// <param name="dbContext"></param>
        public DepartmentRepository(ICurrentUserInfo currentUserInfo, IEFContext dbContext) : base(currentUserInfo, dbContext)
        {

        }

        /// <summary>
        /// 根据条件获取部门列表信息
        /// </summary>
        /// <param name="specification"></param>
        /// <param name="readOnly"></param>
        /// <returns></returns>
        public async Task<List<DepartmentInfoEntity>> GetDepartmentTree(Expression<Func<DepartmentInfoEntity, bool>> specification,bool readOnly)
        {
            var result = new List<DepartmentInfoEntity>();

            var departmentList = await GetAll(readOnly: readOnly)
                        .Where(specification.ReplaceParameter<DepartmentInfoEntity, DepartmentInfo>())
                        .OrderBy(x => x.Sort)
                        .ToListAsync();
            if (departmentList.Any())
            {
                result= departmentList.MapList<DepartmentInfo, DepartmentInfoEntity>();
            }
            return result;
        }
    }
}
